package com.ming.stringstudy.feedbacksystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.stringstudy.feedbacksystem.anno.PermissionLog;
import com.ming.stringstudy.feedbacksystem.entity.CommentType;
import com.ming.stringstudy.feedbacksystem.mapper.CommentTypeMapper;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.service.ICommentTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.stringstudy.feedbacksystem.utils.PermissionConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 问题/意见类型表 服务实现类
 * </p>
 *
 * @author aming
 * @since 2024-12-29
 */
@Service
public class CommentTypeServiceImpl extends ServiceImpl<CommentTypeMapper, CommentType> implements ICommentTypeService {
    @Autowired
    private CommentTypeMapper commentTypeMapper;
    @PermissionLog(PermissionConstants.ISSUE_CATEGORY_MANAGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageBean<CommentType> page(Integer page, Integer pageSize, CommentType commentType) {
        PageHelper.startPage(page, pageSize);

        List<CommentType> commentTypeList = commentTypeMapper.findByAttributes(commentType);
        PageInfo<CommentType> d = new PageInfo<>(commentTypeList);
        PageBean pageBean = new PageBean(d.getTotal(), d.getList());

        return pageBean;
    }
    @PermissionLog(PermissionConstants.ISSUE_CATEGORY_MANAGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addCommentType(CommentType commentType) {
        if (commentType.getDeptId() == null || commentType.getType() == null) {
            return 0;
        }
        if (commentTypeMapper.findByType(commentType.getType()) != null) {
            return 0;
        }

        commentType.setCreateTime(LocalDateTime.now());
        commentType.setUpdateTime(LocalDateTime.now());

        int result = commentTypeMapper.insert(commentType);
        return result;
    }
    @PermissionLog(PermissionConstants.ISSUE_CATEGORY_MANAGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCommentType(CommentType commentType) {
        if (commentTypeMapper.findByType(commentType.getType()) != null) {
            return 0;
        }
        commentType.setUpdateTime(LocalDateTime.now());
        int result = commentTypeMapper.updateById(commentType);
        return result;
    }
    @PermissionLog(PermissionConstants.ISSUE_CATEGORY_MANAGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteCommentType(List<Integer> ids) {
        int result = commentTypeMapper.deleteBatchIds(ids);
        return result;
    }
}
